package backend_0011_3;

import org.w3c.dom.Node;

import java.util.LinkedList;

public class Generic_linkedlist<T> {

    //节点类
    private static class Node<E>{
        Node<E> next;
        E data;
        public Node(E data){
            this.data=data;
            next=null;
        }
    }
    //链表参数
    private int size;
    private Node<T> first;
    public Generic_linkedlist(){
        size=0;
        first=null;
    }
    //添加节点方法
    boolean addList(Node<T> node){
        if(node==null){
            return false;
        }
        if(first==null){
            first=node;
        }
        else{
            Node<T> tail=first;
            while (tail.next!=null){
                tail=tail.next;
            }
            tail.next=node;
        }
        size++;
        return true;
    }
    //删除尾节点
    void removeList0(){
        if(size==0){
            return;
        }
        if(size==1){
            first=null;
            size=0;
            return;
        }
        Node<T> pre=first;
        while (pre.next.next!=null){
            pre=pre.next;
        }
        Node<T> tail=pre.next;
        tail.data=null;
        tail.next=null;
        pre.next=null;
        size--;
    }
    //根据节点值删除节点
    void removelistByValue(T value){
        //处理列表为空
        if(size==0){
            return;
        }
        //处理头节点
        if(first.data.equals(value)){
            Node<T> temp=first;
            first=first.next;
            temp.data=null;
            temp.next=null;
            size--;
            return;
        }
        Node<T> current=first;
        while (current.data!=value){
            current=current.next;
        }
        Node<T> pre=first;
        while (pre.next!=current){
            pre=pre.next;
        }
        pre.next=current.next;
        current.data=null;
        current.next=null;
        size--;
    }
    //根据节点值找到节点下表
    int find(T value){
        Node<T> current=first;
        int i=0;
        while (current!=null){
            i++;
            if(current.data==value){
                return i;
            }
            current=current.next;
        }
        return -1;
    }

    //说明：下段测试代码由豆包生成
    // 测试方法
    public static void main(String[] args) {
        Generic_linkedlist<Integer> list = new Generic_linkedlist<>();

        // 测试添加节点
        list.addList(new Node<>(10));
        list.addList(new Node<>(20));
        list.addList(new Node<>(30));

        // 测试删除尾节点
        list.removeList0();
        System.out.println("删除尾节点后：");
        printList(list); // 应输出 10 20

        // 测试按值删除
        list.removelistByValue(10);
        System.out.println("删除10后：");
        printList(list); // 应输出 20

        // 测试查找
        System.out.println("查找20的索引：" + list.find(20)); // 应输出0
        System.out.println("查找30的索引：" + list.find(30)); // 应输出-1
    }

    // 辅助打印方法
    private static <T> void printList(Generic_linkedlist<T> list) {
        Generic_linkedlist.Node<T> current = list.first;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }
}
